#include "mush.h"
#define MUSH_SPEED 0.05f;

CMush::CMush() : CMovable()
{

}

CMush::~CMush()
{

}

CMush::CMush(int Type,int way,float X, float Y)
{
	reality = 20;
	type = Type;
	v_y = 0;
	movingWay = way;
	v_x = MUSH_SPEED;
	if(way == 0) v_x = - MUSH_SPEED;
	pos_x = X;
	pos_y = Y;
	width = 16;
	height = 20;
}

void CMush::Update(int t)
{
	int ii = BeforeMove();

	pos_x += v_x*t;
	pos_y += v_y*t;
	CheckCol();
	AfterMove(ii);
}

void CMush::RenderFrame(LPDIRECT3DDEVICE9 d3ddv, int t)
{
	int i;
	DWORD now = GetTickCount();
	if(state != 2)		Update(t);
	else
	{
		if(now - last_time > 1000)
		{
			
			life = 0;
			i = mTree->Del(pos_x,pos_y,1,1);
		}
	}
	if (state != 2 && now - last_time > 200)
	{
		anim=(anim+1) % 2;
		last_time = now;
	}
	SpriteHandlerEP->Begin(D3DXSPRITE_ALPHABLEND);
	EnemiesPic->Render(_BackBuffer,pos_x,pos_y,width,height,type,anim);
	SpriteHandlerEP->End();
}

void CMush::isJumpedOn(CObjects * obj)
{
	if(obj->havingStar == 1)
	{
		Die();
		return;
	}
	anim = 2;
	state = 2;
	v_x = 0;
	v_y = 0;
	int i = cTree->Del(pos_x,pos_y,width,height);
	
	height = 16;
	last_time = GetTickCount();
}

void CMush::Touch(CObjects *obj)
{
	if(obj->havingStar == 1)
		Die();
	else
		if(state != 2)
			obj->lvDown();
}

void CMush::Die()
{
	v_y = -0.5f;
	int i = cTree->Del(pos_x,pos_y,width,height);
	i = mTree->Del(pos_x,pos_y,1,1);
	life = 0;
}